package ua.kpi.apolonov.questions;

import java.util.List;

import ua.kpi.apolonov.answers.Answer;
import ua.kpi.apolonov.normalizers.AnswersNormalizers;
import ua.kpi.apolonov.parsers.Parsers;

/**
 * @version 4.0 12.03.2013
 * @author Yegor Apolonov
 */

public class MultipleAnswersQuestion extends Question{

	private AnswersNormalizers normalizer = new AnswersNormalizers();
	private Parsers parser = new Parsers();
	
	public MultipleAnswersQuestion(String question, List<Answer> answers) {
        super(question, answers);
        setInquiry(formatAnswers() + "\nВведите номера правильных ответов\n>> ");
    }
    
	public float inspectAnswers(String userAnswers) {
        float result = 0;
        int[] userNumbers = parser.toIntArrayUserAnswer(userAnswers);      
        for(int answerNumber : userNumbers) {
            int normalUserNumber = normalizer.numberNormalizer(answerNumber);
            if(normalUserNumber >= 0 && normalUserNumber < userNumbers.length) {
            	Answer changedAnswer =  getAnswer(normalUserNumber);
            	if(changedAnswer.getCorrectness()) {
            		result++;
            	}
            } 
        }
        return result;
    }
	
	/*Preparing text of the answers for output*/
	protected String formatAnswers() {
		int numberOfAnswer = 0;
		String formatAnswer = "";
		for(Answer answer : this) {
           	formatAnswer += String.format("\n%d. %s", ++numberOfAnswer, answer.getAnswerText());
        }
		return formatAnswer;
	}
}
